﻿#pragma once
#include <QObject>
#include <QVector>
#include "ADFGlobal.h"
#include "AsTask.h"
#include "AsCamera.h"
#include "AsDetector.h"
#include "AsCommunicator.h"

#include "AsDefine.h"

// 核心业务类
class ADF_EXPORT AsCoreProcess : public QObject {
	Q_OBJECT

public:
	AsCoreProcess(QObject* parent = nullptr);
	virtual ~AsCoreProcess();

private:
	QVector<AsTask*>							m_vpTask;				//所有任务
	bool										m_isRunning;			//运行状态
	QList<AsTask*>								m_vpRecordTask;			//记录的任务

public:
	AsTask* addTask(AsTask* pTask);
	AsTask* addFetchDataTask(int storeKey, AsCamera* pCamera);
	AsTask* addCommuniWaitTask(int waitCommand, AsCommunicator* pCommunicator);
	//添加一个带后置处理的任务,后置任务会在该任务完成时异步开启
	AsTask* addCommuniWaitTaskWithHandleFun(int waitCommand, AsCommunicator* pCommunicator,AsVoidFunc fn);
	AsTask* addMultiMatDetectTask(const QVector<int>& vStoreKey,AsDetector* pDetector);
	AsTask* addMultiTask(const QVector<AsTask*>& vpTask);
	AsTask* addFuncTask(AsVoidFunc fn);
	AsTask* addAsyncFuncTask(AsVoidFunc fn);
	// 重置所有任务状态
	void resetAllTask();

	void clearAllTask();
	inline bool isRunning() { return m_isRunning; }					//获取运行状态

	void recordTask(AsTask* pTask);			//记录任务，用于手动删除

public:
	void startWork();
	void stop();
	void waitForStop();

public:
	template <typename _Type> 
	AsTask* addActionTask(_Type* pUser, void (_Type::*fnName)()) {
		return addTask(new AsActionTask<_Type>(pUser, fnName));
	}

signals:
	void taskFinished(AsTask* pTask);			//任务完成信号
	void stopFinished();
private:

};

